package com.github.fujianlian.klinechart.draw;

import android.graphics.Canvas;
import android.graphics.Paint;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.github.fujianlian.klinechart.BaseKLineChartView;
import com.github.fujianlian.klinechart.ChartHightInfo;
import com.github.fujianlian.klinechart.R;
import com.github.fujianlian.klinechart.base.IValueFormatter;
import com.github.fujianlian.klinechart.entity.h.IRSI;
import com.github.fujianlian.klinechart.formatter.ValueFormatter;

import java.util.List;

//import android.support.annotation.NonNull;
//import android.support.annotation.Nullable;

/**
 * RSI实现类
 * Created by tifezh on 2016/6/19.
 */
public class RSIDraw extends BaseDraw<IRSI,IRSI> {

    private Paint mRSI1Paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint mRSI2Paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint mRSI3Paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    private Paint mLine1Paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint mLine2Paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint mLine3Paint = new Paint(Paint.ANTI_ALIAS_FLAG);


    float lineTop = 70;
    float lineBottom = 30;

    public float getLineTop() {
        return lineTop;
    }

    public void setLineTop(float lineTop) {
        this.lineTop = lineTop;
    }

    public float getLineBottom() {
        return lineBottom;
    }

    public void setLineBottom(float lineBottom) {
        this.lineBottom = lineBottom;
    }
    public RSIDraw() {
        super(IRSI.class);

    }

    public RSIDraw(BaseKLineChartView view) {
        super(IRSI.class);

        init(view);
    }

    @Override
    public void init(BaseKLineChartView view) {
        mLine1Paint.setColor(ContextCompat.getColor(view.getContext(), R.color.chart_red));
        mLine2Paint.setColor(ContextCompat.getColor(view.getContext(), R.color.chart_green));
        mLine3Paint.setColor(ContextCompat.getColor(view.getContext(), R.color.chart_blick));

        mLine1Paint.setStrokeWidth(3);
        mLine2Paint.setStrokeWidth(3);
        mLine3Paint.setStrokeWidth(1);
    }

    @Override
    public void drawTranslated(@Nullable IRSI lastPoint, @NonNull IRSI curPoint, float lastX, float curX, @NonNull Canvas canvas, @NonNull BaseKLineChartView view, int position) {
//        if (lastPoint.getRsi() != 0) {
//            view.drawVolLine(this, canvas, mRSI1Paint, lastX, lastPoint.getRsi(), curX, curPoint.getRsi());
//        }
//
//        view.drawVolLine(this, canvas, mLine1Paint, lastX, 70, curX, 70);
//        view.drawVolLine(this, canvas, mLine2Paint, lastX, 40, curX, 40);
//
//        view.drawVolLine(this, canvas, mLine3Paint, lastX, 60, curX, 60);
//        view.drawVolLine(this, canvas, mLine3Paint, lastX, 30, curX, 30);

    }

    @Override
    public void drawText(@NonNull Canvas canvas, @NonNull BaseKLineChartView view, int position, float x, float y) {
        IRSI point = (IRSI) view.getItem(position);
        if (point.getRsi() != 0) {
            String text = "RSI(14)  ";
            canvas.drawText(text, x, y, view.getTextPaint());
            x += view.getTextPaint().measureText(text);
            text = view.formatValue(point.getRsi());
            canvas.drawText(text, x, y, mRSI1Paint);
        }
    }

    @Override
    public Float getMaxValue(IRSI point) {
        return point.getRsi() < 80 ? 80 : point.getRsi();
    }

    @Override
    public float getMinValue(IRSI point) {
        return point.getRsi() > 20 ? 20 : point.getRsi();
    }

    @Override
    public IValueFormatter getValueFormatter() {
        return new ValueFormatter();
    }

    @Override
    public void drawTranslateds(Canvas canvas, BaseKLineChartView view, List<IRSI> datas, int mStartIndex, int mStopIndex) {
//
//        if (lastPoint.getRsi() != 0) {
//            view.drawVolLine(this, canvas, mRSI1Paint, lastX, lastPoint.getRsi(), curX, curPoint.getRsi());
//        }
//
//        view.drawVolLine(this, canvas, mLine1Paint, lastX, 70, curX, 70);
//        view.drawVolLine(this, canvas, mLine2Paint, lastX, 40, curX, 40);
//
//        view.drawVolLine(this, canvas, mLine3Paint, lastX, 60, curX, 60);
//        view.drawVolLine(this, canvas, mLine3Paint, lastX, 30, curX, 30);

        ChartHightInfo valueInfo = view.getValueInfo(this);
        Float max = valueInfo.getMax();
        Float min = valueInfo.getMin();
        if (lineTop <= max && lineTop >= min) {
            canvas.drawLine(view.getX(mStartIndex), view.getVolY(this, lineTop), view.getX(mStopIndex), view.getVolY(this, lineTop), mLine1Paint);
        }
        if (lineBottom <= max && lineBottom >= min) {
            canvas.drawLine(view.getX(mStartIndex), view.getVolY(this, lineBottom), view.getX(mStopIndex), view.getVolY(this, lineBottom), mLine2Paint);
        }
        canvas.drawLine(view.getX(mStartIndex), view.getVolY(this, 50), view.getX(mStopIndex), view.getVolY(this, 50), mLine3Paint);


//        canvas.drawLine(view.getX(mStartIndex), view.getVolY(this, 40), view.getX(mStopIndex), view.getVolY(this,40), mLine2Paint);
//        canvas.drawLine(view.getX(mStartIndex), view.getVolY(this, 60), view.getX(mStopIndex), view.getVolY(this,60), mLine3Paint);


        float[] pts = new float[(mStopIndex - mStartIndex + 1) * 4];

        for (int i = mStartIndex; i <= mStopIndex; i++) {
            IRSI currentPoint = (IRSI) view.getItem(i);
            float currentPointX = view.getX(i);

            IRSI lastPoint = i == 0 ? currentPoint : (IRSI) view.getItem(i - 1);
            float lastX = i == 0 ? currentPointX : view.getX(i - 1);

            pts[(i - mStartIndex) * 4] = lastX;
            pts[(i - mStartIndex) * 4 + 1] = view.getVolY(this, lastPoint.getRsi());
            pts[(i - mStartIndex) * 4 + 2] = currentPointX;
            pts[(i - mStartIndex) * 4 + 3] = view.getVolY(this, currentPoint.getRsi());
        }

//        view.drawVolLine(this, canvas, mRSI1Paint, lastX, lastPoint.getRsi(), curX, curPoint.getRsi());
        view.drawLines(canvas, mRSI1Paint, pts);

    }

    public void setRSI1Color(int color) {
        mRSI1Paint.setColor(color);
    }

    public void setRSI2Color(int color) {
        mRSI2Paint.setColor(color);
    }

    public void setRSI3Color(int color) {
        mRSI3Paint.setColor(color);
    }

    /**
     * 设置曲线宽度
     */
    public void setLineWidth(float width) {
        mRSI1Paint.setStrokeWidth(width);
        mRSI2Paint.setStrokeWidth(width);
        mRSI3Paint.setStrokeWidth(width);
    }

    /**
     * 设置文字大小
     */
    public void setTextSize(float textSize) {
        mRSI2Paint.setTextSize(textSize);
        mRSI3Paint.setTextSize(textSize);
        mRSI1Paint.setTextSize(textSize);
    }
}
